window._CHAPTER = {

    _MAINLABEL:null,

    showUi:function(parentNode){

        var uiObj = _GRM.getNodeObj('mapSelect');
        var mainLabel = uiObj.node;
        _CHAPTER._MAINLABEL = mainLabel;

        if(uiObj.isnew){
            _CHAPTER.initButton(mainLabel);
        }

        parentNode.addChild(mainLabel);

        _CHAPTER.refreshMap();

        return mainLabel;

    },

    initButton:function(node){

        node.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, node);

        node.getChildByName('bgMap')
        .getChildByName('btnInfo')
        .on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
            _MAP.jumpToMap('如意画卷','下');
        }, node);


        var btns = node.getChildByName('bgMap')
                        .getChildByName('scrollview')
                        .getChildByName('view')
                        .getChildByName('content')
                        .getChildByName('btns')
                        .getChildren();

        for(var i=0;i<btns.length;i++){
            var btn = btns[i];
            btn.on(cc.Node.EventType.TOUCH_START, function(){
                var cityname = this.getChildByName('label').getComponent(cc.Label).string;
                console.log(cityname);
                _MAP.jumpToMap(cityname,'下');
            }, btn);
        }
    },


    refreshMap:function(){

        //_CHAPTER.refreshCurrentPos();

        
        var mainNode = _CHAPTER._MAINLABEL;
        var btns = mainNode.getChildByName('bgMap')
                        .getChildByName('scrollview')
                        .getChildByName('view')
                        .getChildByName('content')
                        .getChildByName('btns')
                        .getChildren();

        var btnshide = mainNode.getChildByName('bgMap')
                        .getChildByName('scrollview')
                        .getChildByName('view')
                        .getChildByName('content')
                        .getChildByName('btnsHide')
                        .getChildren();

        for(var i=0;i<btns.length;i++){
            var cityname = btns[i].getChildByName('label').getComponent(cc.Label).string;
            if(_GAMEDATAHELPER.isChapterOpen(cityname)){
                btns[i].active = true;
            }else{
                btns[i].active = false;
            }
            if(!_MAPCONFIG[cityname]){
                console.log(cityname,'不存在');
            }
        }

        for(var i=0;i<btnshide.length;i++){
            var cityname = btnshide[i].getChildByName('label').getComponent(cc.Label).string;
            if(_GAMEDATAHELPER.isChapterOpen(cityname)){
                btnshide[i].active = false;
            }else{
                btnshide[i].active = true;
            }
            if(!_MAPCONFIG[cityname]){
                console.log(cityname,'不存在');
            }
        }

    },

    //刷新当前位置
    refreshCurrentPos:function(){

        var mainNode = _CHAPTER._MAINLABEL;
        var sign = mainNode.getChildByName('bgMap')
                            .getChildByName('scrollview')
                            .getChildByName('view')
                            .getChildByName('content')
                            .getChildByName('tiledmap')
                            .getChildByName('playerLabel')
                            .getChildByName('currentPos');
        var currentPos = _MAP.playerNode.getPosition();;
        
        sign.setPosition(currentPos);
    },

    



}